﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace RailwayReservationWeb.manager
{
    public partial class train_list : System.Web.UI.Page
    {
        private RailwayReservationDataContext db;

        protected void Page_Load(object sender, EventArgs e)
        {
            db = new RailwayReservationDataContext();
            pMessage.Visible = false;
            if (!String.IsNullOrWhiteSpace(Request.QueryString["q"]))
            {
                if (!ldsTrains.Where.Contains(" && TrainName.Contains(@TrainName)"))
                {
                    ldsTrains.Where = ldsTrains.Where + " && TrainName.Contains(@TrainName)";
                }
            }
            if (!IsPostBack && !String.IsNullOrWhiteSpace(Request.QueryString["q"]))
            {
                txtSearch.Text = Request.QueryString["q"].Trim();
            }
            if (String.IsNullOrWhiteSpace(Request.QueryString["s"])
                || Request.QueryString["s"] != "1")
            {
                btnShow.Text = "Show Deleted";
                if (!btnShow.PostBackUrl.Contains("?s=1"))
                {
                    btnShow.PostBackUrl = btnShow.PostBackUrl + "?s=1";
                }
            }
            else
            {
                if (btnShow.PostBackUrl.Contains("?s=1"))
                {
                    btnShow.PostBackUrl = btnShow.PostBackUrl.Replace("?s=1", "");
                }
                btnShow.Text = "Show Table";
            }
        }

        private void ShowMessage(string msg)
        {
            pMessage.Visible = true;
            pMessage.InnerText = msg;
        }

        protected void btnSearch_Click(object sender, EventArgs e)
        {
            var q = txtSearch.Text.Trim();
            if (!String.IsNullOrWhiteSpace(Request.QueryString["s"]))
            {
                Response.Redirect("~/manager/train_list.aspx?s=" + Request.QueryString["s"] + "&q=" + q);
            }
            else
            {
                Response.Redirect("~/manager/train_list.aspx?q=" + q);
            }
        }

        protected void gvwTrains_RowCommand(object sender, GridViewCommandEventArgs e)
        {
            if (e.CommandName == "AltEdit")
            {
                var index = e.CommandArgument;
                if (index != null)
                {
                    Response.Redirect("~/manager/train_edit.aspx?id=" + index);
                }
            }
            else if (e.CommandName == "AltDelete")
            {
                var index = e.CommandArgument;
                if (index != null)
                {
                    var ob = db.Trains.First(p => p.TrainNo.Equals(index));
                    ob.Status = 0;
                    db.SubmitChanges();
                }
                gvwTrains.DataBind();
            }
            else if (e.CommandName == "AltRestore")
            {
                var index = e.CommandArgument;
                if (index != null)
                {
                    var ob = db.Trains.First(p => p.TrainNo.Equals(index));
                    ob.Status = 1;
                    db.SubmitChanges();
                }
                gvwTrains.DataBind();
            }
        }
    }
}